/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.beetl.ext.fn.IsBlank;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.dao.ZtSpecificationsDao;
import com.jeesite.modules.goods.entity.ZtColor;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.entity.ZtOrder;
import com.jeesite.modules.goods.entity.ZtSpecifications;
import com.jeesite.modules.goods.service.ZtColorService;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.pay.wxpay.api.UUID;
import com.jeesite.modules.test.entity.TestData;
import com.jeesite.modules.test.entity.TestDataChild;

/**
 * 购物车Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztColor")
public class ZtColorController extends BaseController {

	@Autowired
	private ZtColorService ztColorService;
	
	@Autowired
	private ZtGoodsService ztGoodsService;
	
	@Autowired  
    private MyProps myProps;
	@Autowired
	ZtSpecificationsDao ztSpecificationsDao;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtColor get(String id,boolean isNewRecord) {
		//return ztColorService.get(id, isNewRecord);
		
		
		ZtColor ztColor = new ZtColor();
		if (StringUtils.isNotBlank(id)) {
			id=id.split(",")[0];
			ztColor.setId(id);
			ztColor = ztColorService.get(ztColor);
		}
		if (ztColor == null) {
			ztColor = new ZtColor();
		}
		return ztColor;
		}
	
	

	/**
	 * 查询列表
	 */
	
	@RequestMapping(value = { "list", "" })
	public String list(ZtColor ztColor, Model model,HttpSession session) {
		try{
		if(StringUtils.isNotBlank(ztColor.getZtGoods().getId())){
			session.setAttribute("ztColorid", ztColor.getZtGoods().getId());
		}
		}catch(Exception ex){}
		model.addAttribute("ztColor", ztColor);
	
		return "modules/ztColor/ztColorList";
	}
	

	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtColor> listData(ZtColor ztColor, HttpServletRequest request,HttpSession session,
			HttpServletResponse response,Model model) {
		if(ztColor.getZtGoods()==null||StringUtils.isBlank(ztColor.getZtGoods().getId())){
			ZtGoods ztGoods=new ZtGoods();
			
			ztGoods.setId(session.getAttribute("ztColorid").toString());
			ztColor.setZtGoods(ztGoods);
		}
		
		Page<ZtColor> page = ztColorService.findPage(new Page<ZtColor>(request,
				response), ztColor);
		
		
		
		
		return page;
	}
	
	/**
	 * 查看编辑表单
	 */
	
	@RequestMapping(value = "form")
	public String form(ZtColor ztColor, Model model,HttpServletRequest request,HttpServletResponse response) {
		
		model.addAttribute("ztColor", ztColor);	
		
		
		return "modules/ztColor/ztColorForm";
	}

	/**
	 * 保存数据
	 */
	
	@PostMapping(value = "save")
	@ResponseBody
	public String save(ZtColor ztColor,HttpSession session) {
		ztColor.setCreateDate(new Date());
		
		if(ztColor.getZtGoods()==null||StringUtils.isBlank(ztColor.getZtGoods().getId())){
			ZtGoods ztGoods=new ZtGoods();
			String ztGoodsid = session.getAttribute("ztColorid").toString();
			ztGoods.setId(ztGoodsid);

			ztColor.setZtGoods(ztGoods);
			ztGoods=ztGoodsService.get(ztGoods);
			
			if(StringUtils.isBlank(ztColor.getSku())){
				ztColor.setSku(ztGoods.getCode());
			}
			if(StringUtils.isBlank(ztColor.getPname())){
				ztColor.setPname(ztGoods.getTitle());
			}
		}
		
		
		ztColorService.save(ztColor);
		return renderResult(Global.TRUE, text("保存成功！"));
	}

	
	/**
	 * 删除数据
	 */

	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtColor ztColor) {
		ztColorService.delete(ztColor);
		return renderResult(Global.TRUE, text("删除成功！"));
	}
}